package meijuli.com.join51.business.resetpwd

import com.jhx.common.http.ApiManager
import com.jhx.common.http.HttpRequest
import com.jhx.common.http.HttpResponse
import com.jhx.common.http.ResponseCallback
import meijuli.com.join51.business.service.ApiService
import meijuli.com.join51.business.utils.StringUtil
import retrofit2.Call

/**
 * 重置登录密码
 *
 * @author Jack 2018-01-20 17:18
 */
class ResetPwdPresenterImpl : Contract.ResetPasswordPresenter() {
    private var mCall: Call<HttpResponse<String>>? = null

    override fun resetPwd(oldPwd: String, newPwd: String) {
        mCall = ApiManager.get().getApiService(ApiService::class.java).resetLoginPwd(StringUtil.encodeToSHA256(oldPwd), StringUtil.encodeToSHA256(newPwd))
        mView?.showLoading()
        val httpclient = HttpRequest.getRequest()
        if(mView?.context() != null) {
            httpclient.autoRecycle(mView?.context()!!)
        }
        httpclient.start(mCall, object: ResponseCallback<String>() {
            override fun onSuccess(url: String?, data: String?) {
                mView?.hideLoading()
                mView?.resetPwdSuccess()
            }

            override fun onError(url: String?, errorCode: Int, errorMsg: String?) {
                super.onError(url, errorCode, errorMsg)
                mView?.hideLoading()
            }
        })
    }

    override fun recycler() {
        mCall?.cancel()
        mView = null
    }
}